package com.winway.sixbook.ui;

import java.util.Calendar;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;
import com.winway.sixbook.custom.CallAlarm;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class NextWyAlarmActivity extends Activity {
	  TextView setTime1;
	  Button mButton1;
	  Button mButton2;
	  
	  String time1String = null;
	  String time2String = null;
	  String time3String = null;
	  String defalutString = "目前无设置";
	  
	  LinearLayout linearLayout1 ;
	  LinearLayout linearLayout2 ;
	  
	  private ImageButton top_tbn_fanhui;
	  private Button top_tbn_xinzeng;
	  private TextView title;
	  
	  Calendar c=Calendar.getInstance();

	  @Override
	  public void onCreate(Bundle savedInstanceState){
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_next_wy_alarm);
	    
	    top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        top_tbn_fanhui.setOnClickListener(mListener);
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
        title.setText("我要添加");
      
	    //取得活动的Preferences对象
	    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
	    time1String = settings.getString("TIME1", defalutString);
	    
//	    InitButton1();
//	    InitButton2();
	    SetAlarn();
	    jxtj();
	    
	    if(null != time1String){
	    	setTime1.setText(time1String);
	    }
	    
	    
	  }
	  
	  OnClickListener mListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.nagivate_btn_left:
					Intent counter = new Intent(NextWyAlarmActivity.this, HomeActivity.class);
					startActivity(counter);
					finish();
					break;
				}
			}
		};
	  
	  public void SetAlarn(){
		  setTime1=(TextView) findViewById(R.id.setTime1);
		  linearLayout1 = (LinearLayout)findViewById(R.id.nextalarm_layout1);
		  linearLayout1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
	    		c.setTimeInMillis(System.currentTimeMillis());
	    		int mHour=c.get(Calendar.HOUR_OF_DAY);
	    		int mMinute=c.get(Calendar.MINUTE);
	    		
	    		new TimePickerDialog(NextWyAlarmActivity.this, new TimePickerDialog.OnTimeSetListener(){           
	    			@Override
	    			public void onTimeSet(TimePicker view,int hourOfDay,int minute){
	    				c.setTimeInMillis(System.currentTimeMillis());
	    				c.set(Calendar.HOUR_OF_DAY,hourOfDay);
	    				c.set(Calendar.MINUTE,minute);
	    				c.set(Calendar.SECOND,0);
	    				c.set(Calendar.MILLISECOND,0);
	              
	    				Intent intent = new Intent(NextWyAlarmActivity.this, CallAlarm.class);
	    				PendingIntent sender=PendingIntent.getBroadcast(NextWyAlarmActivity.this,0, intent, 0);
	    				AlarmManager am;
	    				am = (AlarmManager)getSystemService(ALARM_SERVICE);
	    				am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender);
	    				String tmpS=format(hourOfDay)+"："+format(minute);
	    				setTime1.setText(tmpS);
	              
	    				//SharedPreferences保存数据，并提交
	    				SharedPreferences time1Share = getPreferences(0);
	    				SharedPreferences.Editor editor = time1Share.edit();
	    				editor.putString("TIME1", tmpS);
	    				editor.commit();
	              
	    				Toast.makeText(NextWyAlarmActivity.this,"设置提醒时间为"+tmpS, Toast.LENGTH_SHORT).show();
	    			}          
	    		},mHour,mMinute,true).show();
	      
			}
		});
	  }
	  
	  public void jxtj(){
		  linearLayout2 = (LinearLayout)findViewById(R.id.nextalarm_layout2);
		  linearLayout2.setOnClickListener(new OnClickListener() {
			  @Override
				public void onClick(View v) {
				  Intent counter4 = new Intent(NextWyAlarmActivity.this, GridViewChooose.class);
				  startActivity(counter4);
				  finish();
			  }
		  });
		  
	  }
	  
//	  public void InitButton1(){
//		    setTime1=(TextView) findViewById(R.id.setTime1);
//		    mButton1=(Button)findViewById(R.id.mButton1);
//		    mButton1.setOnClickListener(new View.OnClickListener(){
//		    	public void onClick(View v){
//		    		c.setTimeInMillis(System.currentTimeMillis());
//		    		int mHour=c.get(Calendar.HOUR_OF_DAY);
//		    		int mMinute=c.get(Calendar.MINUTE);
//		    		
//		    		new TimePickerDialog(NextWyAlarmActivity.this, new TimePickerDialog.OnTimeSetListener(){           
//		    			@Override
//		    			public void onTimeSet(TimePicker view,int hourOfDay,int minute){
//		    				c.setTimeInMillis(System.currentTimeMillis());
//		    				c.set(Calendar.HOUR_OF_DAY,hourOfDay);
//		    				c.set(Calendar.MINUTE,minute);
//		    				c.set(Calendar.SECOND,0);
//		    				c.set(Calendar.MILLISECOND,0);
//		              
//		    				Intent intent = new Intent(NextWyAlarmActivity.this, CallAlarm.class);
//		    				PendingIntent sender=PendingIntent.getBroadcast(NextWyAlarmActivity.this,0, intent, 0);
//		    				AlarmManager am;
//		    				am = (AlarmManager)getSystemService(ALARM_SERVICE);
//		    				am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender);
//		    				String tmpS=format(hourOfDay)+"："+format(minute);
//		    				setTime1.setText(tmpS);
//		              
//		    				//SharedPreferences保存数据，并提交
//		    				SharedPreferences time1Share = getPreferences(0);
//		    				SharedPreferences.Editor editor = time1Share.edit();
//		    				editor.putString("TIME1", tmpS);
//		    				editor.commit();
//		              
//		    				Toast.makeText(NextWyAlarmActivity.this,"设置提醒时间为"+tmpS, Toast.LENGTH_SHORT).show();
//		    			}          
//		    		},mHour,mMinute,true).show();
//		      }
//		    });
//	  }
//
//	  public void InitButton2()
//	  {
//		    mButton2=(Button) findViewById(R.id.mButton2);
//		    mButton2.setOnClickListener(new View.OnClickListener()
//		    {
//		      public void onClick(View v)
//		      {
//		        Intent intent = new Intent(NextWyAlarmActivity.this, CallAlarm.class);
//		        PendingIntent sender=PendingIntent.getBroadcast(NextWyAlarmActivity.this,0, intent, 0);
//		        AlarmManager am;
//		        am =(AlarmManager)getSystemService(ALARM_SERVICE);
//		        am.cancel(sender);
//		        Toast.makeText(NextWyAlarmActivity.this,"提醒删除",
//		                       Toast.LENGTH_SHORT).show();
//		        setTime1.setText("目前无设置");
//		        
//		        SharedPreferences time1Share = getPreferences(0);
//	            SharedPreferences.Editor editor = time1Share.edit();
//	            editor.putString("TIME1", "目前无设置");
//	            editor.commit();
//		      }
//		    });
//	  }

	private String format(int x)
	  {
	    String s=""+x;
	    if(s.length()==1) s="0"+s;
	    return s;
	  }
}
